if( SERVER ) then
   
 	AddCSLuaFile( "shared.lua" ) 
 	SWEP.HoldType = "pistol";
 
 end 

 
 SWEP.Base = "ts2_base";
   
 SWEP.Spawnable			= false 
 SWEP.AdminSpawnable		= false 
   


SWEP.Primary.Sound = Sound( "Weapon_SMG1.Single" );

SWEP.ViewModel = Model( "models/weapons/v_stunstick.mdl" );
SWEP.WorldModel = Model( "models/weapons/W_stunbaton.mdl" );

SWEP.PrintName = "Uncharged Baton";
SWEP.TS2Desc = "Very light and tough, but uncharged";

 SWEP.ItemWidth = 3;
 SWEP.ItemHeight = 1;

SWEP.IconCamPos = Vector( 38, 86, 33 ) 
SWEP.IconLookAt = Vector( 0, 0, 0 ) 
SWEP.IconFOV = 6

 SWEP.Primary.ViewPunchMul = .5;
 SWEP.Primary.Damage			= 5 
 SWEP.Primary.NumShots		= 1 
 
 SWEP.TS2HoldType = "BLUNT";

SWEP.Slot = 2;

SWEP.Primary.ClipSize = 0;
SWEP.Primary.DefaultClip = 0;
SWEP.Primary.Delay = .07;
SWEP.Primary.Automatic = true;

 SWEP.Primary.IronSightPos = Vector( 0, 5.5, -5.0 );
 SWEP.Primary.IronSightAng = Vector( 0.0, 0.0, 0.0 );

SWEP.Primary.HolsteredPos = Vector( -2.4, -4, -18.0 );

SWEP.PrimaryCharge = true;

SWEP.Primary.ChargePos = Vector( 0.0, 15.0, -5.0 );

SWEP.HitSounds = {

	Sound( "weapons/stunstick/stunstick_impact2.wav" )
}

SWEP.SwingSounds = {

	Sound( "weapons/stunstick/stunstick_swing1.wav" ),
	Sound( "weapons/stunstick/stunstick_swing2.wav" )

}

SWEP.LastSwing = 0;
SWEP.IsCharging = false;

function SWEP:RealPrimaryAttack( ply )
	
	self.Weapon:EmitSound( self.SwingSounds[math.random( 1, #self.SwingSounds )] );
			
	local trace = { }
	trace.start = ply:EyePos();
	trace.endpos = trace.start + ply:GetAimVector() * 50;
	trace.filter = ply;
	
	local tr = util.TraceLine( trace );
	
	if( tr.Hit ) then

		self.Weapon:EmitSound( self.HitSounds[math.random( 1, #self.HitSounds )] );

	end
	
	if( SERVER ) then
	
		if( self.IsCharging ) then

			if( tr.Entity and tr.Entity:IsValid() ) then
			
				local perc = GetPercentage( "chargestunbaton", ply ) / 100;
				local norm = ( ply:EyePos() - tr.HitPos ):Normalize();
			
				if( not tr.Entity:IsPlayer() ) then
					tr.Entity:GetPhysicsObject():ApplyForceOffset( norm * -9000 * math.Clamp( perc, .4, 1 ), tr.HitPos );
				else
					tr.Entity:HandleStunStickHit( ply, perc, tr.HitGroup, self.LastAttack );
				end
				
			end
	
			self.Weapon:SendWeaponAnim( ACT_VM_HITCENTER );
			DestroyProcessBar( "chargestunbaton", ply );
			
			ply:GetTable().ForcedAnimationMode = true;
			ply:GetTable().ForcedAnimation = 277;
			
			timer.Simple( .6, function() ply:GetTable().ForcedAnimationMode = false; end );
		
		end
		
	else
	
		RunConsoleCommand( "eng_dorealprimaryattack", "" );
	
	end
	
	self.LastSwing = CurTime();
	self.IsCharging = false;

end

function SWEP:CanCharge()

	if( CurTime() - self.LastSwing < .8 ) then
		return false;
	end
	
	return true;

end

SWEP.LastAttack = 0;

function SWEP:PrimaryAttack()

	if( not self:CanCharge() ) then
		return;
	end
	
	if( SERVER and self.Owner:GetPlayerHolstered() ) then
		return;
	end
	
	if( SERVER and self.Owner:IsTied() ) then return; end 
	
	if( self.IsCharging ) then
		return;
	end
	
	self.IsCharging = true;

	if( SERVER ) then
	
		self.LastAttack = 1;
	
		CreateProcessBar( "chargestunbaton", "Building swing..", self.Owner );
			SetEstimatedTime( .8, self.Owner );
			SetThinkDelay( .1, self.Owner );
			SetColor( Color( 110, 0, 0, 200 ), self.Owner );
			SetLinger( true, self.Owner );
		EndProcessBar( self.Owner );
	
	end

end

if( CLIENT ) then

	function SWEP:DrawHUD()
	
		if( not ClientVars["Holstered"] ) then
		
			draw.DrawText( "+", "NewChatFont", ScrW() / 2, ScrH() / 2, Color( 255, 255, 255, 200 ), 1, 1 );
		
		end
	
	end

end